/**
 * JungoMobileWeb
 * AjaxUtil.java
 * 
 * Copyright 2013 Teachizen
 * Written by cloudstick
 *	2013. 7. 27. first written.
 *
 * note:
 */
package com.jungo.web.util;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import lombok.Cleanup;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @author cloudstick
 *
 */
@Slf4j
public class AjaxUtil {
	private static HttpServletResponse getResponse(HttpServletResponse response) {
		response.setContentType("text/html; charset=utf-8"); 
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Cache-Control", "no-cache, must-revalidate");
		response.setHeader("Cache-Control", "no-store");
		response.setDateHeader("Expires", 0);
		return response;
	}
	
	public static void printMessage(HttpServletResponse response, JSONObject jsonObj) {
		try {
			@Cleanup PrintWriter out = getResponse(response).getWriter();
			out.print(jsonObj);
			out.flush();
		} catch (Exception e) {
			log.error("Exception occured.", e);
		}
	}
	
	public static void printMessage(HttpServletResponse response, JSONArray jsonArray) {
		try {
			@Cleanup PrintWriter out = getResponse(response).getWriter();
			out.print(jsonArray);
			out.flush();
		} catch (Exception e) {
			log.error("Exception occured.", e);
		}
	}
}
